import { nmAppLog } from '@app-nextmesh/common/utils';
import { arrowChange } from '@app-nextmesh/utils/drawTensor';
import { documentV2 } from '@app-nextmesh/domain';

/**
 * 张量图信息
 */
type TensorParams = {
  type: string;
  field: string;
  systemType: string;
  index: number;
  step: number;
  frame: number;
  direction: string[];
};

// 获取后端数据
const createRenderTensor = async (
  type: string,
  checkedValues: string[],
  resolvedData: string,
  resultstSelect,
) => {
  const params: TensorParams = {
    type: type,
    systemType: resolvedData,
    index: resultstSelect.step,
    step: resultstSelect.step,
    frame: resultstSelect.step,
    field: resultstSelect.field,
    direction: type === 'principal' ? resultstSelect.variable : checkedValues,
  };
  nmAppLog('getRenderTensor--------->:', params, checkedValues, resultstSelect);
  const req = await documentV2.renderTensor(resultstSelect.model, params);
  nmAppLog('req--------->:', req);
  if (req?.tensor) {
    const tensorReq = req.tensor;
    if (type === 'component') {
      tensorReq.component.forEach((item) => {
        arrowChange(
          tensorReq.centerPoint,
          tensorReq.length,
          tensorReq.max,
          tensorReq.min,
          item.value,
          item.direction,
          item?.vector,
          resolvedData,
        );
      });
    } else if (checkedValues?.length !== 0) {
      tensorReq.principal.forEach((item) => {
        checkedValues.map((checked) => {
          nmAppLog('principal-------->:', item[checked], checked);
          arrowChange(
            tensorReq.centerPoint,
            tensorReq.length,
            tensorReq.max,
            tensorReq.min,
            item[checked].value,
            item.direction,
            item[checked]?.vector,
            resolvedData,
            checked,
          );
        });
      });
    }
  }
};

export { createRenderTensor };
